#!/bin/bash

PVE_BASE_PATH=$1

docker(){
    if [[ -f '/usr/bin/docker' ]];then
	/usr/bin/docker -v
	echo "Docker already installed.Nothing to do."
	exit
    fi
    cName="docker"
    version="20.10.9"
    url1="https://download.docker.com/linux/static/stable/x86_64/docker-"$version".tgz"
    url2="http://d.pvenas.com:8080/api/public/dl/lUAEnLZW/docker-"$version".tgz"
    url3="https://download.docker.com/linux/static/stable/x86_64/docker-"$version".tgz"
    url4="https://download.docker.com/linux/static/stable/x86_64/docker-"$version".tgz"
    url5="https://download.docker.com/linux/static/stable/x86_64/docker-"$version".tgz"
    url6="https://download.docker.com/linux/static/stable/x86_64/docker-"$version".tgz"
#--base-funcs-start--
    apt install -y axel
    echo "Initializing download..."
    axel -n 5 -av -s 2048000 -o $cName.tgz $url6
    tar -xvf $cName.tgz
    chmod +x docker/*
    cp docker/* $PVE_BASE_PATH/usr/bin/
    echo "config docker start..."

    cat << EOF > $PVE_BASE_PATH/etc/systemd/system/docker.service
[Unit]
Description=pvenasDocker
After=syslog.targer network.target

[Service]
Type=simple

ExecStart=/usr/bin/dockerd
ExecStop=//bin/killall dockerd
ExecStop=/bin/kill -9 \$MAINPID

User=root
Group=root

[Install]
WantedBy=multi-user.target
EOF
    if [ ! -d "/etc/docker" ];then
        mkdir /etc/docker
    fi
    mkdir $PVE_BASE_PATH/etc/docker
    cat << EOF > $PVE_BASE_PATH/etc/docker/daemon.json
{
    "registry-mirrors": [
        "https://mirror.ccs.tencentyun.com",
        "https://registry.docker-cn.com",
        "https://dockerhub.azk8s.cn",
        "https://reg-mirror.qiniu.com",
        "http://hub-mirror.c.163.com",
        "https://3laho3y3.mirror.aliyuncs.com",
        "http://f1361db2.m.daocloud.io"
    ],
    "graph": "/var/lib/docker"
}
EOF
    chmod +x $PVE_BASE_PATH/usr/bin/*
    rm -rf docker/
}

docker
